﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Excellent
{
    public partial class AdministratorForm : Form
    {
        public AdministratorForm()
        {
            InitializeComponent();
        }

        private void 添加学生成绩ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddForm A = new AddForm();
            A.Show();
            
        }

     

        private void tBstudents_Click(object sender, EventArgs e)
        {
            StudentlistForm A = new StudentlistForm();
            A.Show();
        }

        private void 学生信息列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InformationlistForm I = new InformationlistForm();
            I.Show();
        }

        private void 科目列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CourselistForm c = new CourselistForm();
            c.Show();
        }

        private void tBTheuser_Click(object sender, EventArgs e)
        {
            TheeditorForm td = new TheeditorForm();
            td.Show();
        }

        private void 学生用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TheuserForm tf = new TheuserForm();
            tf.Show();
        }

        private void tBname_Click(object sender, EventArgs e)
        {
            string naem = this.textBox1.Text.Trim();
            string sql = string.Format(@"select * from StudentUsers a,T_gacct b where  a.gradeid=b.gradeid" 
                                + " and name like  '%{0}%'", naem);
            DataSet ds = DBHelper.GetDataSet(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.dataGridView1.DataSource = dt;
            }
        }

        private void tBgrade_Click(object sender, EventArgs e)
        {
            string grade = this.textBox1.Text.Trim();
            string sql = string.Format(@"select * from StudentUsers a,T_gacct b where  a.gradeid=b.gradeid" 
                                    + " and  grade like  '{0}%'", grade);
            DataSet ds = DBHelper.GetDataSet(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.dataGridView1.DataSource = dt;
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
             //获取当前行的所有值
             //获取idCurrentRow:当前选中行的id Ce11s[o]:第一个Value:值
            string id = this.dataGridView1.CurrentRow.Cells["id"].Value.ToString();
            string name = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            string sex = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
            string gradeid = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
            string phone = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
            string address = this.dataGridView1.CurrentRow.Cells[5].Value.ToString();
            string dateofbirth = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();
            string email = this.dataGridView1.CurrentRow.Cells[7].Value.ToString();

            grade.Studentid = Convert.ToInt32(id);
            grade.name = name;
            grade.Sex = sex;
            grade.gradeid = gradeid;
            grade.phone = Convert.ToInt32(phone);
            grade.address = address;
            grade.dateofbirth = Convert.ToDateTime(dateofbirth);
            grade.email = email;


            modifyForm s = new modifyForm();
            s.Show();
        }

        private void AdministratorForm_Load(object sender, EventArgs e)
        {
            string sql = string.Format(@"select * from StudentUsers a,T_gacct b where  a.gradeid=b.gradeid");
            DataSet ds = DBHelper.GetDataSet(sql);
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string sql = string.Format(@"select * from StudentUsers a,T_gacct b where  a.gradeid=b.gradeid");
            DataSet ds = DBHelper.GetDataSet(sql);
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int UserID = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
            string sql = string.Format("delete from StudentUsers where Studentid='{0}'", UserID);
            bool row = DBHelper.ExecuteNonQuery(sql);
            if (row)
            {
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }
    }
}
